package quiz;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CreateQuestionServlet
 */
@WebServlet("/CreateQuestion")
public class CreateQuestionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateQuestionServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dpatch = request.getRequestDispatcher("create-question.jsp");
		dpatch.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//question text changes based on the question type.  
		//In the case of a fill-in-the-blank, we separate the halves with an underscore.
		int questionType = Integer.parseInt((String)request.getParameter("type"));
		String questionText;
		switch(questionType){
		case Question.FITB:
			questionText = (String)request.getParameter("before") + "_" + (String)request.getParameter("after");
			break;
		case Question.PICTURE:
			questionText = (String)request.getParameter("picture");
			break;
		default:
			questionText = (String)request.getParameter("question");
			break;
		}
		
		if (questionText.isEmpty() || questionText.equals("_")){
			request.setAttribute("error", "You didn't type a question!");
			RequestDispatcher dpatch = request.getRequestDispatcher("create-question.jsp");
			dpatch.forward(request, response);
			return;
		}
		
		int questionWeight;
		try {questionWeight = Integer.parseInt((String)request.getParameter("weight"));}
		catch (NumberFormatException e){
			request.setAttribute("error", "You didn't enter a valid question weight!");
			RequestDispatcher dpatch = request.getRequestDispatcher("create-question.jsp");
			dpatch.forward(request, response);
			return;
		}
		if (questionWeight <= 0){
			request.setAttribute("error", "You didn't enter a valid question weight!");
			RequestDispatcher dpatch = request.getRequestDispatcher("create-question.jsp");
			dpatch.forward(request, response);
			return;
		}
		
		/*int time;
		try {time = Integer.parseInt((String)request.getParameter("time"));}
		catch (NumberFormatException e){
			request.setAttribute("error", "You didn't enter a valid time limit!");
			RequestDispatcher dpatch = request.getRequestDispatcher("create-question.jsp");
			dpatch.forward(request, response);
			return;
		}*/
		
		int numAnswers = Integer.parseInt((String)request.getParameter("numberAnswers"));
		int quizID = Integer.parseInt((String)request.getParameter("quizID"));
		
		DataBase db = (DataBase)request.getServletContext().getAttribute("db");
		Quiz quiz = db.getQuiz(quizID);
		int questionsSoFar = quiz.getQuestions().size();
		Question q = new Question(quizID, questionText, questionType, 0, questionsSoFar + 1, questionWeight);
		db.storeQuestion(q);
		
		for (int i = 1; i <= numAnswers; i++){
			
			String ithAnswerText = (String)request.getParameter("answer"+i);
			boolean is_correct;
			
			if (questionType == Question.MC) is_correct = (request.getParameter("answer" + i + "correct") != null);
			else is_correct = true;
		
			db.storeAnswer(new Answer(q.getID(), ithAnswerText, Answer.TEXT, is_correct, i));
		}
		
		request.setAttribute("quiz", db.getQuiz(quizID));
		RequestDispatcher dpatch = request.getRequestDispatcher("add-questions.jsp");
		dpatch.forward(request, response);
	}

}
